#! /bin/bash
<<comment
关闭selinux 防火墙 配置yum源 注意镜像文件挂载路径  本脚本适用于centos7.9 其他自测  默认root密码123
需满足预装机与PXE服务器在同一网段
如需自动配置装机后服务，可通过kickstart图形软件自行编写安装后脚本生成应答文件替换本脚本应答文件内容（44-149行内容）
comment
subnet=192.168.88.0
netmask=255.255.255.0
range="192.168.88.20 192.168.88.90"
gateway=192.168.88.254
IP=192.168.88.50  # DHCP服务器
iso_down=/mydvd   # 镜像文件挂载路径
echo "----------安装相应软件包--------------"
yum -y install dhcp tftp-server vsftpd  syslinux &> /dev/null
echo "----------配置DHCP服务---------------"
cat >> /etc/dhcp/dhcpd.conf <<EOF
subnet $subnet netmask $netmask {
  range $range;
  option domain-name-servers 8.8.8.8;
  option routers $gateway;
  default-lease-time 600;
  max-lease-time 7200;
  next-server  $IP;
  filename     "pxelinux.0";
}
EOF
systemctl enable dhcpd tftp vsftpd --now &> /dev/null
echo "-----------准备开机引导文件------------" 
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ && mkdir /var/lib/tftpboot/pxelinux.cfg # 部署pxelinux.0二进制文件
cp $iso_down/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default  # cp菜单文件
cd $iso_down/isolinux/
cp vesamenu.c32  splash.png vmlinuz initrd.img /var/lib/tftpboot/ && cd - &> /dev/null # cp图形模块、背景图片、启动内核、驱动程序
# 修改default菜单文件
sed -i '11s/CentOS 7/YJY PXE SERVER/' /var/lib/tftpboot/pxelinux.cfg/default
sed -i '62a\  menu default' /var/lib/tftpboot/pxelinux.cfg/default
sed -i "s,inst.stage2.*,ks=ftp://$IP/ks.cfg,"  /var/lib/tftpboot/pxelinux.cfg/default && sed -i '66,$d' /var/lib/tftpboot/pxelinux.cfg/default
mkdir /var/ftp/PXE-CENTOS  # 创建镜像挂载目录
cat >> /etc/fstab <<EOF
/dev/cdrom /var/ftp/PXE-CENTOS iso9660 defaults 0 0
EOF
# 写入应答文件   可通过system-config-kickstart图形软件生成应答文件
if mount -a; then
cat >> /var/ftp/ks.cfg <<EOF
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$qzAcFKkV\$IJlV.mjoEZiEuappCyqOV/
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="ftp://$IP/PXE-CENTOS"
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="xfs" --grow --size=1

%packages
@base
-abrt-addon-ccpp
-abrt-addon-python
-abrt-cli
-abrt-console-notification
-bash-completion
-blktrace
-bpftool
-bridge-utils
-bzip2
-chrony
-cryptsetup
-dmraid
-dosfstools
-ethtool
-fprintd-pam
-gnupg2
-hunspell
-hunspell-en
-kmod-kvdo
-kpatch
-ledmon
-libaio
-libreport-plugin-mailx
-libstoragemgmt
-lvm2
-man-pages
-man-pages-overrides
-mdadm
-mlocate
-mtr
-nano
-ntpdate
-pinfo
-plymouth
-pm-utils
-rdate
-rfkill
-rng-tools
-rsync
-scl-utils
-setuptool
-smartmontools
-sos
-sssd-client
-strace
-sysstat
-systemtap-runtime
-tcpdump
-tcsh
-teamd
-time
-unzip
-usbutils
-vdo
-vim-enhanced
-virt-what
-wget
-which
-words
-xfsdump
-xz
-yum-langpacks
-yum-utils
-zip

%end
EOF
else
echo "镜像文件挂载失败"
sleep 10
echo "PXE网络装机服务配置失败"
exit
fi
echo "PXE网络装机服务已就绪"
